extends NamedNode


class_name NamedComponent
"""
子组件编辑器(绑定组件到特定字段上)
"""


## 从子节点中获取
static func get_in_children(node: Node, items: Dictionary) -> void:
    for c in node.get_children():
        if c is NamedComponent:
            var sub_items: Dictionary = c.get_items()
            for k in sub_items:
                var desc: String = "%s.%s" % [c.name, k]
                items[desc] = sub_items[k]
        elif c is INamedNode:
            items.merge(c.get_items())
        else:
            items[c.name] = c


## 从节点属性字段中获取
static func get_in_node(wrapper: Node, items: Dictionary) -> void:
    for f in NodeHelper.get_custom_fields(wrapper):
        if f in ["is_enable", "is_frozen", "is_node_frozen"]:
            continue
        var v: Variant = wrapper.get(f)
        if v is INamedNode:
            items.merge(v.get_items())
        else:
            items[f] = v


func __check_exports() -> Dictionary:
    return IComponent.merge({}, super.__check_exports(), ["wrapper"])

func get_reference() -> Node:
    return self

func get_items() -> Dictionary:
    var items: Dictionary = {}
    # 子节点
    NamedComponent.get_in_children(self, items)
    # 需要反射的节点
    if wrapper:
        NamedComponent.get_in_node(wrapper, items)
    return items

func subassembly(cmpt_name: String) -> Node:
    var root: Node = self
    for value in cmpt_name.split('.'):
        for c in root.get_children():
            if c.name == value:
                if c is NamedComponent:
                    root = c
                    break
                elif c is NamedNode:
                    return c.get_reference()
                else:
                    return c
    return null
